<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class RefundClassCommand extends Command
{
    protected $signature = 'make:refund';
    protected $description = '机动组-创建统一售后表-命令';


    /**
     * FunctionName：handle
     * Description：
     * Author：lwl
     */
    public function handle()
    {
        try {
            $project = config('app.name');
            $smallInteger = 'table->smallInteger';
            $table = '$table';
            $bigIncrements = '$table->bigIncrements';
            $tinyInteger = '$table->tinyInteger';
            $string = '$table->string';
            $unsignedTinyInteger = '$table->unsignedTinyInteger';
            $unsignedBigInteger = '$table->unsignedBigInteger';
            $decimal = '$table->decimal';
            $unique = '$table->unique';
            $dateTime = '$table->dateTime';
            $randNumber = rand(100000,999999);
            $content = <<<EOF
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableCityAgentRefunds extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('{$project}_refunds', function (Blueprint $table) {
            $bigIncrements('id');
            $string('order_sn',30)->default('')->comment('订单编号');
            $string('refund_sn',30)->default('')->comment('退款单编号');
            $unsignedBigInteger('member_id')->default(0)->comment('会员id');
            $decimal('refund_money',10,4)->default(0)->comment('退款金额');
            $unsignedTinyInteger('refund_way')->default(1)->comment('实际退款方式：1:原来退还,2:余额退还;3：线下打款;4:无支付退款（报单，三方订单等没有真实支付的退款）');
            $smallInteger('status')->default(10)->comment('售后状态：10申请中  20退款中 30完成 -10退款失败 -20 财务驳回');
            $dateTime('finished_at')->nullable()->comment('退款完成时间');
            $dateTime('created_at')->nullable()->comment('创建日期');
            $dateTime('updated_at')->nullable()->comment('更新日期');
            $dateTime('deleted_at')->nullable()->comment('删除日期');
        });
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `{$project}_refunds` COMMENT='退单表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('{$project}_refunds');
    }
}


EOF;
            $res = fopen(database_path("migrations/2021_07_12_{$randNumber}_create_refund_table.php"), 'w');
            fputs($res, $content);
            fclose($res);
            $this->info('创建成功');
        }catch (\Exception $exception){
            $this->error('创建失败');
        }
    }
}
